package com.example.demo.service;

import com.example.demo.dao.DemoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service
public class DemoService {

    @Autowired
    private DemoDao demoDao;

    /**
     * 字典列表
     *
     * @return
     * @throws Exception
     */
    public List<Map> loadDictList(Map param) throws Exception {
        return demoDao.loadDictList(param);
    }


    /**
     * 字典列表
     *
     * @return
     * @throws Exception
     */
    public void savadata(Map param) throws Exception {
        demoDao.insertdata(param);
    }


    /**
     * 试题一
     *
     * @return
     * @throws Exception
     */
    public List<Map> examtest1(Map param) throws Exception {
        return demoDao.examtest1(param);
    }

    /**
     * 试题二
     *
     * @return
     * @throws Exception
     */
    public List<LinkedHashMap<String, String>> examtest2(Map<String, String> param) {
        return demoDao.examtest2(param);
    }

    public LinkedHashMap<String, Object> examtest3(Map<String, String> param) {
        //数据处理
        List<LinkedHashMap<String, String>> resultD = demoDao.examtest3(param);
        LinkedHashSet<String> datakeyset = new LinkedHashSet<>();

        //获取key
        resultD.forEach(new Consumer<LinkedHashMap<String, String>>() {
            @Override
            public void accept(LinkedHashMap<String, String> smap) {
                datakeyset.add(String.valueOf(smap.get("tjrq")));
            }
        });


        //拼接数据
        LinkedHashMap<String, Object> slinkedhashmaps = new LinkedHashMap<String, Object>();
        for (String key : datakeyset) {
            LinkedList<Map<String, Object>> objects = new LinkedList<>();

            for (LinkedHashMap<String, String> smap : resultD) {
                String tjrq = String.valueOf(smap.get("tjrq"));
                String tjlb = smap.get("tjlb") != null ? smap.get("tjlb") : "";
                String tjlbmc = smap.get("tjlbmc") != null ? smap.get("tjlbmc") : "";
                String tjnum = smap.get("tjnum") != null ? String.valueOf(smap.get("tjnum")) : "";
                if (key.equals(tjrq)) {
                    LinkedHashMap<String, Object> temp = new LinkedHashMap<>();
                    temp.put("tjlb", tjlb);
                    temp.put("tjlbmc", tjlbmc);
                    temp.put("tjnum", tjnum);
                    objects.add(temp);
                }
            }
            slinkedhashmaps.put(key, objects);
        }


        return slinkedhashmaps;
    }

    public List<Map<String,String>> examtest4(Map<String, String> param) {
        LinkedHashMap<String, Object> rsfind = examtest3(param);

        //平铺数据
        ArrayList<Map<String, String>> mapslist = mapslist = new ArrayList<>();
        ;
        for (Map.Entry<String, Object> entry : rsfind.entrySet()) {
            String key = entry.getKey();
            List<Map<String, String>> valuelist = (List<Map<String, String>>) entry.getValue();

            for (Map<String, String> smap : valuelist) {
                smap.put("tjrq", key);
                mapslist.add(smap);
            }

        }

        //比较
        List<Map<String, String>> collect = mapslist.stream().sorted(new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                BigDecimal b1 = new BigDecimal(o1.get("tjnum"));
                BigDecimal b2 = new BigDecimal(o2.get("tjnum"));
                return b2.compareTo(b1);
            }
        }).limit(1).collect(Collectors.toList());

        return collect;
    }
}
